package com.xiyue.leaspring.util.dbc;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

public class RedisConnectionUtil4 {

	private static final int TIME_OUT = 2000;
	private static final int SO_TIMEOUT = 100;
	private static final int MAX_ATTEMPTS = 2000;
	private static final int MAX_TOTAL = 200;
	private static final int MAX_IDLE = 20;
	private static final int MAX_WAIT_MILLIS = 1000;
	private static final boolean TEST_ON_BORROW = true;
	private JedisCluster jedisCluster;
	
	public RedisConnectionUtil4() {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(MAX_TOTAL);
		config.setMaxIdle(MAX_IDLE);
		config.setMaxWaitMillis(MAX_WAIT_MILLIS);
		config.setTestOnBorrow(TEST_ON_BORROW);
		Set<HostAndPort> allRedisCluster = new HashSet<HostAndPort>();
		allRedisCluster.add(new HostAndPort("redis-cluter-a",6379));
		allRedisCluster.add(new HostAndPort("redis-cluter-a",6380));
		allRedisCluster.add(new HostAndPort("redis-cluter-a",6381));
		allRedisCluster.add(new HostAndPort("redis-cluter-b",6379));
		allRedisCluster.add(new HostAndPort("redis-cluter-b",6380));
		allRedisCluster.add(new HostAndPort("redis-cluter-b",6381));
		allRedisCluster.add(new HostAndPort("redis-cluter-c",6379));
		allRedisCluster.add(new HostAndPort("redis-cluter-c",6380));
		allRedisCluster.add(new HostAndPort("redis-cluter-c",6381));
	
		this.jedisCluster = new JedisCluster(allRedisCluster,TIME_OUT,SO_TIMEOUT
				,MAX_ATTEMPTS,config);
	}
	
	public void close() {
		try {
			this.jedisCluster.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public JedisCluster getConnection() {
		return this.jedisCluster;
	}
	
}
